package com.hyperfix.washer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyperfix.common.exception.CustomException;
import com.hyperfix.models.common.R;
import com.hyperfix.models.washer.pojos.Station;
import com.hyperfix.models.washer.pojos.Team;
import com.hyperfix.models.washer.vos.TeamVO;
import com.hyperfix.washer.mapper.StationMapper;
import com.hyperfix.washer.mapper.TeamMapper;
import com.hyperfix.washer.service.TeamService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JiangChunYu
 * 2024/4/21
 */
@Transactional
@Service
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements TeamService {
    @Resource
    private TeamMapper teamMapper;
    @Resource
    private StationMapper stationMapper;

    @Override
    public R<IPage<Team>> list(Integer pageNum, Integer size) {
        IPage<Team> teamIPage = new Page<>(pageNum, size);
        teamIPage = teamMapper.listTeam(teamIPage);
        return R.success(teamIPage);
    }

    @Override
    public R<String> updateTeam(TeamVO teamVO) {
        boolean isUpdated = teamMapper.updateTeam(teamVO);
        if (!isUpdated) {
            throw  new CustomException("井队信息修改失败，井队不存在");
        }
        return R.success("井队信息修改成功");
    }

    @Override
    public R<List<Station>> listStation(Integer teamId) {
        List<Station> stations = stationMapper.selectList(new LambdaQueryWrapper<Station>().eq(Station::getTeamId, teamId));
        return R.success(stations);
    }
}
